{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime\n",
    "import copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>查询,入库,出库,移库,盘点,设置</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               value\n",
       "0  查询,入库,出库,移库,盘点,设置"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例一（行转列）\n",
    "# 读取数据集\n",
    "df1 = pd.read_excel('./示例数据.xlsx',sheet_name='data1')\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    查询\n",
       "1    入库\n",
       "2    出库\n",
       "3    移库\n",
       "4    盘点\n",
       "5    设置\n",
       "Name: value, dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一：使用 explode 方法\n",
    "df1_result = df1.value.str.split(',').explode('value')\n",
    "df1_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间</th>\n",
       "      <th>计划开始时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间              计划开始时间\n",
       "0   A   备布     1   120 2021-02-20 10:00:00\n",
       "1   A   染色     2   480                 NaT\n",
       "2   A   烘干     3   120                 NaT\n",
       "3   A   晒布     4    60                 NaT\n",
       "4   A   定型     5   120                 NaT\n",
       "5   A   检验     6    60                 NaT\n",
       "6   A   入库     7    60                 NaT\n",
       "7   B   备布     1   120 2021-02-21 00:00:00\n",
       "8   B  预定型     2   180                 NaT\n",
       "9   B   染色     3   480                 NaT\n",
       "10  B   烘干     4   120                 NaT\n",
       "11  B   晒布     5    60                 NaT\n",
       "12  B   定型     6   120                 NaT\n",
       "13  B   检验     7    60                 NaT\n",
       "14  B   入库     8    60                 NaT"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例二（时间相加填充）\n",
    "# 读取数据集\n",
    "df2 = pd.read_excel('./示例数据.xlsx',sheet_name='data2')\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间</th>\n",
       "      <th>计划开始时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间              计划开始时间\n",
       "0   A   备布     1   120 2021-02-20 10:00:00\n",
       "1   A   染色     2   480 2021-02-20 10:00:00\n",
       "2   A   烘干     3   120 2021-02-20 10:00:00\n",
       "3   A   晒布     4    60 2021-02-20 10:00:00\n",
       "4   A   定型     5   120 2021-02-20 10:00:00\n",
       "5   A   检验     6    60 2021-02-20 10:00:00\n",
       "6   A   入库     7    60 2021-02-20 10:00:00\n",
       "7   B   备布     1   120 2021-02-21 00:00:00\n",
       "8   B  预定型     2   180 2021-02-21 00:00:00\n",
       "9   B   染色     3   480 2021-02-21 00:00:00\n",
       "10  B   烘干     4   120 2021-02-21 00:00:00\n",
       "11  B   晒布     5    60 2021-02-21 00:00:00\n",
       "12  B   定型     6   120 2021-02-21 00:00:00\n",
       "13  B   检验     7    60 2021-02-21 00:00:00\n",
       "14  B   入库     8    60 2021-02-21 00:00:00"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向上填充缺失日期\n",
    "df2_copy = copy.deepcopy(df2)\n",
    "df2_copy = df2_copy.fillna(method='ffill')\n",
    "df2_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间</th>\n",
       "      <th>计划开始时间</th>\n",
       "      <th>分组累加-加工时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>1020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间              计划开始时间  分组累加-加工时间\n",
       "0   A   备布     1   120 2021-02-20 10:00:00        120\n",
       "1   A   染色     2   480 2021-02-20 10:00:00        600\n",
       "2   A   烘干     3   120 2021-02-20 10:00:00        720\n",
       "3   A   晒布     4    60 2021-02-20 10:00:00        780\n",
       "4   A   定型     5   120 2021-02-20 10:00:00        900\n",
       "5   A   检验     6    60 2021-02-20 10:00:00        960\n",
       "6   A   入库     7    60 2021-02-20 10:00:00       1020\n",
       "7   B   备布     1   120 2021-02-21 00:00:00        120\n",
       "8   B  预定型     2   180 2021-02-21 00:00:00        300\n",
       "9   B   染色     3   480 2021-02-21 00:00:00        780\n",
       "10  B   烘干     4   120 2021-02-21 00:00:00        900\n",
       "11  B   晒布     5    60 2021-02-21 00:00:00        960\n",
       "12  B   定型     6   120 2021-02-21 00:00:00       1080\n",
       "13  B   检验     7    60 2021-02-21 00:00:00       1140\n",
       "14  B   入库     8    60 2021-02-21 00:00:00       1200"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用自定义函数，做时间的加法\n",
    "df2_copy['分组累加-加工时间'] = df2_copy.groupby(['产品'])['加工时间'].cumsum()\n",
    "df2_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间</th>\n",
       "      <th>计划开始时间</th>\n",
       "      <th>分组累加-加工时间</th>\n",
       "      <th>时间相加</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>600</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>720</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>1020</td>\n",
       "      <td>2021-02-21 03:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>300</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1080</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1140</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1200</td>\n",
       "      <td>2021-02-21 20:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间              计划开始时间  分组累加-加工时间                时间相加\n",
       "0   A   备布     1   120 2021-02-20 10:00:00        120 2021-02-20 12:00:00\n",
       "1   A   染色     2   480 2021-02-20 10:00:00        600 2021-02-20 20:00:00\n",
       "2   A   烘干     3   120 2021-02-20 10:00:00        720 2021-02-20 22:00:00\n",
       "3   A   晒布     4    60 2021-02-20 10:00:00        780 2021-02-20 23:00:00\n",
       "4   A   定型     5   120 2021-02-20 10:00:00        900 2021-02-21 01:00:00\n",
       "5   A   检验     6    60 2021-02-20 10:00:00        960 2021-02-21 02:00:00\n",
       "6   A   入库     7    60 2021-02-20 10:00:00       1020 2021-02-21 03:00:00\n",
       "7   B   备布     1   120 2021-02-21 00:00:00        120 2021-02-21 02:00:00\n",
       "8   B  预定型     2   180 2021-02-21 00:00:00        300 2021-02-21 05:00:00\n",
       "9   B   染色     3   480 2021-02-21 00:00:00        780 2021-02-21 13:00:00\n",
       "10  B   烘干     4   120 2021-02-21 00:00:00        900 2021-02-21 15:00:00\n",
       "11  B   晒布     5    60 2021-02-21 00:00:00        960 2021-02-21 16:00:00\n",
       "12  B   定型     6   120 2021-02-21 00:00:00       1080 2021-02-21 18:00:00\n",
       "13  B   检验     7    60 2021-02-21 00:00:00       1140 2021-02-21 19:00:00\n",
       "14  B   入库     8    60 2021-02-21 00:00:00       1200 2021-02-21 20:00:00"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间 加 分钟\n",
    "df2_copy['时间相加'] = df2_copy['计划开始时间'] + pd.to_timedelta(df2_copy['分组累加-加工时间'],unit='m')\n",
    "df2_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间</th>\n",
       "      <th>计划开始时间</th>\n",
       "      <th>分组累加-加工时间</th>\n",
       "      <th>时间相加</th>\n",
       "      <th>时间下移</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>600</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>720</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>1020</td>\n",
       "      <td>2021-02-21 03:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>300</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1080</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1140</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1200</td>\n",
       "      <td>2021-02-21 20:00:00</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间              计划开始时间  分组累加-加工时间                时间相加  \\\n",
       "0   A   备布     1   120 2021-02-20 10:00:00        120 2021-02-20 12:00:00   \n",
       "1   A   染色     2   480 2021-02-20 10:00:00        600 2021-02-20 20:00:00   \n",
       "2   A   烘干     3   120 2021-02-20 10:00:00        720 2021-02-20 22:00:00   \n",
       "3   A   晒布     4    60 2021-02-20 10:00:00        780 2021-02-20 23:00:00   \n",
       "4   A   定型     5   120 2021-02-20 10:00:00        900 2021-02-21 01:00:00   \n",
       "5   A   检验     6    60 2021-02-20 10:00:00        960 2021-02-21 02:00:00   \n",
       "6   A   入库     7    60 2021-02-20 10:00:00       1020 2021-02-21 03:00:00   \n",
       "7   B   备布     1   120 2021-02-21 00:00:00        120 2021-02-21 02:00:00   \n",
       "8   B  预定型     2   180 2021-02-21 00:00:00        300 2021-02-21 05:00:00   \n",
       "9   B   染色     3   480 2021-02-21 00:00:00        780 2021-02-21 13:00:00   \n",
       "10  B   烘干     4   120 2021-02-21 00:00:00        900 2021-02-21 15:00:00   \n",
       "11  B   晒布     5    60 2021-02-21 00:00:00        960 2021-02-21 16:00:00   \n",
       "12  B   定型     6   120 2021-02-21 00:00:00       1080 2021-02-21 18:00:00   \n",
       "13  B   检验     7    60 2021-02-21 00:00:00       1140 2021-02-21 19:00:00   \n",
       "14  B   入库     8    60 2021-02-21 00:00:00       1200 2021-02-21 20:00:00   \n",
       "\n",
       "                  时间下移  \n",
       "0                  NaT  \n",
       "1  2021-02-20 12:00:00  \n",
       "2  2021-02-20 20:00:00  \n",
       "3  2021-02-20 22:00:00  \n",
       "4  2021-02-20 23:00:00  \n",
       "5  2021-02-21 01:00:00  \n",
       "6  2021-02-21 02:00:00  \n",
       "7                  NaT  \n",
       "8  2021-02-21 02:00:00  \n",
       "9  2021-02-21 05:00:00  \n",
       "10 2021-02-21 13:00:00  \n",
       "11 2021-02-21 15:00:00  \n",
       "12 2021-02-21 16:00:00  \n",
       "13 2021-02-21 18:00:00  \n",
       "14 2021-02-21 19:00:00  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在组中下移一行值\n",
    "df2_copy['时间下移'] = df2_copy.groupby(['产品'])['时间相加'].shift(1)\n",
    "df2_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间_x</th>\n",
       "      <th>计划开始时间_x</th>\n",
       "      <th>分组累加-加工时间</th>\n",
       "      <th>时间相加</th>\n",
       "      <th>时间下移</th>\n",
       "      <th>加工时间_y</th>\n",
       "      <th>计划开始时间_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "      <td>NaT</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>600</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>720</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>1020</td>\n",
       "      <td>2021-02-21 03:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>NaT</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>300</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>180</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1080</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1140</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1200</td>\n",
       "      <td>2021-02-21 20:00:00</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间_x            计划开始时间_x  分组累加-加工时间                时间相加  \\\n",
       "0   A   备布     1     120 2021-02-20 10:00:00        120 2021-02-20 12:00:00   \n",
       "1   A   染色     2     480 2021-02-20 10:00:00        600 2021-02-20 20:00:00   \n",
       "2   A   烘干     3     120 2021-02-20 10:00:00        720 2021-02-20 22:00:00   \n",
       "3   A   晒布     4      60 2021-02-20 10:00:00        780 2021-02-20 23:00:00   \n",
       "4   A   定型     5     120 2021-02-20 10:00:00        900 2021-02-21 01:00:00   \n",
       "5   A   检验     6      60 2021-02-20 10:00:00        960 2021-02-21 02:00:00   \n",
       "6   A   入库     7      60 2021-02-20 10:00:00       1020 2021-02-21 03:00:00   \n",
       "7   B   备布     1     120 2021-02-21 00:00:00        120 2021-02-21 02:00:00   \n",
       "8   B  预定型     2     180 2021-02-21 00:00:00        300 2021-02-21 05:00:00   \n",
       "9   B   染色     3     480 2021-02-21 00:00:00        780 2021-02-21 13:00:00   \n",
       "10  B   烘干     4     120 2021-02-21 00:00:00        900 2021-02-21 15:00:00   \n",
       "11  B   晒布     5      60 2021-02-21 00:00:00        960 2021-02-21 16:00:00   \n",
       "12  B   定型     6     120 2021-02-21 00:00:00       1080 2021-02-21 18:00:00   \n",
       "13  B   检验     7      60 2021-02-21 00:00:00       1140 2021-02-21 19:00:00   \n",
       "14  B   入库     8      60 2021-02-21 00:00:00       1200 2021-02-21 20:00:00   \n",
       "\n",
       "                  时间下移  加工时间_y            计划开始时间_y  \n",
       "0                  NaT     120 2021-02-20 10:00:00  \n",
       "1  2021-02-20 12:00:00     480                 NaT  \n",
       "2  2021-02-20 20:00:00     120                 NaT  \n",
       "3  2021-02-20 22:00:00      60                 NaT  \n",
       "4  2021-02-20 23:00:00     120                 NaT  \n",
       "5  2021-02-21 01:00:00      60                 NaT  \n",
       "6  2021-02-21 02:00:00      60                 NaT  \n",
       "7                  NaT     120 2021-02-21 00:00:00  \n",
       "8  2021-02-21 02:00:00     180                 NaT  \n",
       "9  2021-02-21 05:00:00     480                 NaT  \n",
       "10 2021-02-21 13:00:00     120                 NaT  \n",
       "11 2021-02-21 15:00:00      60                 NaT  \n",
       "12 2021-02-21 16:00:00     120                 NaT  \n",
       "13 2021-02-21 18:00:00      60                 NaT  \n",
       "14 2021-02-21 19:00:00      60                 NaT  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匹配两表\n",
    "df2_merge = pd.merge(df2_copy,df2,on=['产品','工序','加工顺序'],how='left')\n",
    "df2_merge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间_x</th>\n",
       "      <th>计划开始时间_x</th>\n",
       "      <th>分组累加-加工时间</th>\n",
       "      <th>时间相加</th>\n",
       "      <th>时间下移</th>\n",
       "      <th>加工时间_y</th>\n",
       "      <th>计划开始时间_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>600</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>720</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "      <td>1020</td>\n",
       "      <td>2021-02-21 03:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>300</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "      <td>180</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>780</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "      <td>480</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>900</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>960</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1080</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "      <td>120</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1140</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "      <td>1200</td>\n",
       "      <td>2021-02-21 20:00:00</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "      <td>60</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间_x            计划开始时间_x  分组累加-加工时间                时间相加  \\\n",
       "0   A   备布     1     120 2021-02-20 10:00:00        120 2021-02-20 12:00:00   \n",
       "1   A   染色     2     480 2021-02-20 10:00:00        600 2021-02-20 20:00:00   \n",
       "2   A   烘干     3     120 2021-02-20 10:00:00        720 2021-02-20 22:00:00   \n",
       "3   A   晒布     4      60 2021-02-20 10:00:00        780 2021-02-20 23:00:00   \n",
       "4   A   定型     5     120 2021-02-20 10:00:00        900 2021-02-21 01:00:00   \n",
       "5   A   检验     6      60 2021-02-20 10:00:00        960 2021-02-21 02:00:00   \n",
       "6   A   入库     7      60 2021-02-20 10:00:00       1020 2021-02-21 03:00:00   \n",
       "7   B   备布     1     120 2021-02-21 00:00:00        120 2021-02-21 02:00:00   \n",
       "8   B  预定型     2     180 2021-02-21 00:00:00        300 2021-02-21 05:00:00   \n",
       "9   B   染色     3     480 2021-02-21 00:00:00        780 2021-02-21 13:00:00   \n",
       "10  B   烘干     4     120 2021-02-21 00:00:00        900 2021-02-21 15:00:00   \n",
       "11  B   晒布     5      60 2021-02-21 00:00:00        960 2021-02-21 16:00:00   \n",
       "12  B   定型     6     120 2021-02-21 00:00:00       1080 2021-02-21 18:00:00   \n",
       "13  B   检验     7      60 2021-02-21 00:00:00       1140 2021-02-21 19:00:00   \n",
       "14  B   入库     8      60 2021-02-21 00:00:00       1200 2021-02-21 20:00:00   \n",
       "\n",
       "                  时间下移  加工时间_y            计划开始时间_y  \n",
       "0  2021-02-20 10:00:00     120 2021-02-20 10:00:00  \n",
       "1  2021-02-20 12:00:00     480                 NaT  \n",
       "2  2021-02-20 20:00:00     120                 NaT  \n",
       "3  2021-02-20 22:00:00      60                 NaT  \n",
       "4  2021-02-20 23:00:00     120                 NaT  \n",
       "5  2021-02-21 01:00:00      60                 NaT  \n",
       "6  2021-02-21 02:00:00      60                 NaT  \n",
       "7  2021-02-21 00:00:00     120 2021-02-21 00:00:00  \n",
       "8  2021-02-21 02:00:00     180                 NaT  \n",
       "9  2021-02-21 05:00:00     480                 NaT  \n",
       "10 2021-02-21 13:00:00     120                 NaT  \n",
       "11 2021-02-21 15:00:00      60                 NaT  \n",
       "12 2021-02-21 16:00:00     120                 NaT  \n",
       "13 2021-02-21 18:00:00      60                 NaT  \n",
       "14 2021-02-21 19:00:00      60                 NaT  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将 NAT 日期替换为另一列中的数据\n",
    "df2_merge['时间下移'] = df2_merge['时间下移'].combine_first(df2_merge['计划开始时间_y'])\n",
    "# df2_merge['时间下移'] = np.where(df2_merge['时间下移'].notnull(),df2_merge['时间下移'],df2_merge['计划开始时间_y'])\n",
    "df2_merge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品</th>\n",
       "      <th>工序</th>\n",
       "      <th>加工顺序</th>\n",
       "      <th>加工时间_x</th>\n",
       "      <th>时间下移</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 10:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>染色</td>\n",
       "      <td>2</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-20 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>烘干</td>\n",
       "      <td>3</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 20:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>晒布</td>\n",
       "      <td>4</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-20 22:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>定型</td>\n",
       "      <td>5</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-20 23:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>A</td>\n",
       "      <td>检验</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 01:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>入库</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>备布</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>预定型</td>\n",
       "      <td>2</td>\n",
       "      <td>180</td>\n",
       "      <td>2021-02-21 02:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>染色</td>\n",
       "      <td>3</td>\n",
       "      <td>480</td>\n",
       "      <td>2021-02-21 05:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>烘干</td>\n",
       "      <td>4</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 13:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>B</td>\n",
       "      <td>晒布</td>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 15:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>B</td>\n",
       "      <td>定型</td>\n",
       "      <td>6</td>\n",
       "      <td>120</td>\n",
       "      <td>2021-02-21 16:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>B</td>\n",
       "      <td>检验</td>\n",
       "      <td>7</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 18:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>B</td>\n",
       "      <td>入库</td>\n",
       "      <td>8</td>\n",
       "      <td>60</td>\n",
       "      <td>2021-02-21 19:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   产品   工序  加工顺序  加工时间_x                时间下移\n",
       "0   A   备布     1     120 2021-02-20 10:00:00\n",
       "1   A   染色     2     480 2021-02-20 12:00:00\n",
       "2   A   烘干     3     120 2021-02-20 20:00:00\n",
       "3   A   晒布     4      60 2021-02-20 22:00:00\n",
       "4   A   定型     5     120 2021-02-20 23:00:00\n",
       "5   A   检验     6      60 2021-02-21 01:00:00\n",
       "6   A   入库     7      60 2021-02-21 02:00:00\n",
       "7   B   备布     1     120 2021-02-21 00:00:00\n",
       "8   B  预定型     2     180 2021-02-21 02:00:00\n",
       "9   B   染色     3     480 2021-02-21 05:00:00\n",
       "10  B   烘干     4     120 2021-02-21 13:00:00\n",
       "11  B   晒布     5      60 2021-02-21 15:00:00\n",
       "12  B   定型     6     120 2021-02-21 16:00:00\n",
       "13  B   检验     7      60 2021-02-21 18:00:00\n",
       "14  B   入库     8      60 2021-02-21 19:00:00"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最终结果\n",
    "df2_result = df2_merge[['产品','工序','加工顺序','加工时间_x','时间下移']]\n",
    "df2_result = df2_result.rename({'加工时间_x':'加工时间','时间下移':'计划开始时间'})\n",
    "df2_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>分数</th>\n",
       "      <th>部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>甲</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>乙</td>\n",
       "      <td>80</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>丙</td>\n",
       "      <td>60</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>丁</td>\n",
       "      <td>90</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>戊</td>\n",
       "      <td>69</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>己</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>庚</td>\n",
       "      <td>70</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号 姓名  分数 部门\n",
       "0   1  甲  85  A\n",
       "1   2  乙  80  B\n",
       "2   3  丙  60  B\n",
       "3   4  丁  90  A\n",
       "4   5  戊  69  A\n",
       "5   6  己  85  A\n",
       "6   7  庚  70  A"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例三（输出部门分数前两名的姓名和分数）\n",
    "# 读取数据集\n",
    "df3_1 = pd.read_excel('./示例数据.xlsx',sheet_name='data3_1')\n",
    "df3_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>部门</th>\n",
       "      <th>部门名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>IT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>销售</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  部门 部门名\n",
       "0  A  IT\n",
       "1  B  销售"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3_2 = pd.read_excel('./示例数据.xlsx',sheet_name='data3_2')\n",
    "df3_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>分数</th>\n",
       "      <th>部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>丁</td>\n",
       "      <td>90</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>己</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>甲</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>庚</td>\n",
       "      <td>70</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>戊</td>\n",
       "      <td>69</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>乙</td>\n",
       "      <td>80</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>丙</td>\n",
       "      <td>60</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号 姓名  分数 部门\n",
       "3   4  丁  90  A\n",
       "5   6  己  85  A\n",
       "0   1  甲  85  A\n",
       "6   7  庚  70  A\n",
       "4   5  戊  69  A\n",
       "1   2  乙  80  B\n",
       "2   3  丙  60  B"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组排序\n",
    "df3_1 = df3_1.sort_values(by=['部门','分数','姓名'],ascending=[True,False,True])\n",
    "df3_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>分数</th>\n",
       "      <th>部门</th>\n",
       "      <th>降序组内排名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>丁</td>\n",
       "      <td>90</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>己</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>甲</td>\n",
       "      <td>85</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>庚</td>\n",
       "      <td>70</td>\n",
       "      <td>A</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>戊</td>\n",
       "      <td>69</td>\n",
       "      <td>A</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>乙</td>\n",
       "      <td>80</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>丙</td>\n",
       "      <td>60</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号 姓名  分数 部门  降序组内排名\n",
       "3   4  丁  90  A       1\n",
       "5   6  己  85  A       2\n",
       "0   1  甲  85  A       2\n",
       "6   7  庚  70  A       3\n",
       "4   5  戊  69  A       4\n",
       "1   2  乙  80  B       1\n",
       "2   3  丙  60  B       2"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 降序组内排名\n",
    "df3_1['降序组内排名'] = df3_1['分数'].groupby(df3_1['部门']).rank(method='dense',ascending=False).astype(int)\n",
    "df3_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>部门名</th>\n",
       "      <th>姓名</th>\n",
       "      <th>分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>IT</td>\n",
       "      <td>丁</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>IT</td>\n",
       "      <td>己</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>IT</td>\n",
       "      <td>甲</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>IT</td>\n",
       "      <td>庚</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>销售</td>\n",
       "      <td>乙</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>销售</td>\n",
       "      <td>丙</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  部门名 姓名  分数\n",
       "0  IT  丁  90\n",
       "1  IT  己  85\n",
       "2  IT  甲  85\n",
       "3  IT  庚  70\n",
       "5  销售  乙  80\n",
       "6  销售  丙  60"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匹配 部门名\n",
    "df3_merge = pd.merge(df3_1,df3_2,on='部门',how='left')\n",
    "df3_merge = df3_merge[df3_merge['降序组内排名'] <= 3]\n",
    "df3_result = df3_merge[['部门名','姓名','分数']]\n",
    "df3_result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
